package cn.iocoder.yudao.module.erp.service.business.order.orderdelivery;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.business.order.orderdelivery.vo.OrderDeliveryPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.order.orderdelivery.vo.OrderDeliverySaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.business.order.orderdelivery.OrderDeliveryDO;
import cn.iocoder.yudao.module.erp.dal.mysql.business.order.orderdelivery.OrderDeliveryMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.ORDER_DELIVERY_NOT_EXISTS;

/**
 * 交货方式 Service 实现类
 *
 * @author yy
 */
@Service
@Validated
public class OrderDeliveryServiceImpl implements OrderDeliveryService {

    @Resource
    private OrderDeliveryMapper orderDeliveryMapper;

    @Override
    public Long createOrderDelivery(OrderDeliverySaveReqVO createReqVO) {
        // 插入
        OrderDeliveryDO orderDelivery = BeanUtils.toBean(createReqVO, OrderDeliveryDO.class);
        orderDeliveryMapper.insert(orderDelivery);
        // 返回
        return orderDelivery.getId();
    }

    @Override
    public void updateOrderDelivery(OrderDeliverySaveReqVO updateReqVO) {
        // 校验存在
        validateOrderDeliveryExists(updateReqVO.getId());
        // 更新
        OrderDeliveryDO updateObj = BeanUtils.toBean(updateReqVO, OrderDeliveryDO.class);
        orderDeliveryMapper.updateById(updateObj);
    }

    @Override
    public void deleteOrderDelivery(Long id) {
        // 校验存在
        validateOrderDeliveryExists(id);
        // 删除
        orderDeliveryMapper.deleteById(id);
    }

    private void validateOrderDeliveryExists(Long id) {
        if (orderDeliveryMapper.selectById(id) == null) {
            throw exception(ORDER_DELIVERY_NOT_EXISTS);
        }
    }

    @Override
    public OrderDeliveryDO getOrderDelivery(Long id) {
        return orderDeliveryMapper.selectById(id);
    }

    @Override
    public PageResult<OrderDeliveryDO> getOrderDeliveryPage(OrderDeliveryPageReqVO pageReqVO) {
        return orderDeliveryMapper.selectPage(pageReqVO);
    }

}